import numpy as np
from scipy.ndimage.interpolation import shift


class Filter:
    def __init__(self, size, ganma, channel):
        self.size = size
        self.channel = channel
        # self.ganma=ganma
        self.filterdata = np.zeros(size * channel).reshape(channel, size)  # 新的数据排在最前面
        self.ganma = [ganma * np.power(1 - ganma, i - 1) for i in range(size)]
        # self.ganma[0]=1
        # print("init...")
        # print("filterdata=",self.filterdata)
        # print("ganma=",self.ganma)

    def Clear(self):
        self.filterdata = np.zeros(self.size * self.channel).reshape(self.channel, self.size)

    def Add(self, data):
        # data=[x,y,z,..]
        self.filterdata = np.roll(self.filterdata, 1, axis=1)  # 移位
        self.filterdata[:, 0] = 0  # 清零
        self.filterdata[:, 0] = self.filterdata[:, 0] + data  # 加入新值
        # print("ADD:",self.filterdata)
        None

    def GetResult(self):
        result = np.zeros(self.channel)
        result = np.dot(self.filterdata, self.ganma)
        # print("GetResult=",result)
        return result  # 返回array[x,y,z..]


F = Filter(size=3, ganma=0.9, channel=2)
F.Add([1, 2])
F.GetResult()
F.Add([3, 4])
F.GetResult()
F.Add([5, 6])
F.GetResult()
F.Add([7, 8])
